# coding: utf-8
"""
将链接转换为html，含图片（多进程）
"""
import glob
import json
import multiprocessing
import os
import time

from tqdm import tqdm
from wechatarticles import Url2Html


def verify_url(article_url):
    verify_lst = ["mp.weixin.qq.com", "__biz", "mid", "sn", "idx"]
    for string in verify_lst:
        if string not in article_url:
            return False
    return True


def copyright_num(copyright_stat):
    if copyright_stat == 11:
        return 1  # 标记原创
    else:
        return 0


uh = Url2Html()


def run(item):
    url, fj, title, date, proxies, root_path = item
    try:
        res = uh.run(
            url,
            mode=4,
            account=fj,
            title=title,
            date=date,
            proxies=proxies,
            img_path=root_path,
        )
        if res != 0:
            time.sleep(5)
    except Exception as e:
        print(e)
        time.sleep(5)


if __name__ == "__main__":
    fj_lst = glob.glob("*.json")
    # 设定代理
    proxies_lst = [
        {"http": "socks5://127.0.0.1:10808", "https": "socks5://127.0.0.1:10808"},
        {"http": "socks5://127.0.0.1:3003", "https": "socks5://127.0.0.1:3003"},
        None,
    ]
    pi = 0
    for fj in fj_lst:
        fj = fj.split(".json")[0]

        if not os.path.isdir(fj):
            os.mkdir(fj)

        root_path = os.path.join(fj, "imgs")
        if not os.path.isdir(root_path):
            os.mkdir(root_path)

        with open(fj + ".json", "r", encoding="utf-8") as f:
            data = f.readlines()

        # data = data[::-1]
        print(len(data))
        flag = 0
        lst = []
        for line in tqdm(data):
            item = json.loads(line.strip()[1:-1], strict=False)

            timestamp = item["comm_msg_info"]["datetime"]
            # if timestamp < end_timestamp:
            #     break
            date = uh.timestamp2date(timestamp)

            infos = item["app_msg_ext_info"]

            url_title_lst = [[infos["content_url"], infos["title"], 1, infos]]
            if infos["is_multi"] == 1:
                url_title_lst += [
                    [info["content_url"], info["title"], i, info]
                    for i, info in enumerate(infos["multi_app_msg_item_list"], 2)
                ]

            for url, title, headlines, info_ in url_title_lst:
                if not verify_url(url):
                    continue

                copyright_stat = copyright_num(info_["copyright_stat"])
                # if copyright_stat != 1:
                #     continue
                lst.append([url, fj, title, date, proxies_lst[pi], root_path])
                pi += 1
                if pi == len(proxies_lst):
                    pi = 0

            if flag == 1:
                break

        pool_num = multiprocessing.cpu_count()
        pool_num = len(proxies_lst)

        with multiprocessing.Pool(pool_num) as p:
            res = list(tqdm(p.imap(run, lst), total=len(lst), desc="进度"))

        p.close()
        p.join()
